from rest_framework import serializers
from .models import *

class ProductPicSerializer(serializers.ModelSerializer):
    class Meta:
        model=ProductPic
        exclude='product',

class ProductStyleSerializer(serializers.ModelSerializer):
    class Meta:
        model=ProductStyle
        exclude='product',


class ProductSerializer(serializers.ModelSerializer):
    gender=serializers.SerializerMethodField()
    type = serializers.SerializerMethodField()
    category = serializers.SerializerMethodField()
    pic = ProductPicSerializer(many=True,label='图片列表')
    style=ProductStyleSerializer(many=True,label='款式列表')
    size=serializers.SerializerMethodField()
    class Meta:
        model=Product
        fields='__all__'
    def get_gender(self,obj):
        return obj.get_gender_display()
    def get_type(self,obj):
        return obj.get_type_display()
    def get_category(self,obj):
        return obj.category.title
    def get_size(self,obj):
        size=[]
        if obj.size:
            size=obj.size.split('|')
        return size


